import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from '@/views/Reg.vue'
import Login from '@/views/Login.vue'
import Main from '@/views/Main.vue'
import store from '@/store'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/reg', component: Reg },
    { path: '/login', component: Login },
    { path: '/', component: Main }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/reg' || to.path === '/login') {
    next()
  } else {
    if (store.state.muser.token === '') {
      next('/login')
    } else {
      next()
    }
  }
})

export default router
